package com.cmcc.driver.ui.main.adapter

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.cmcc.shared.model.Msg
import com.cmcc.driver.databinding.ListItemDriverMsgBinding

class MsgAdapter : ListAdapter<Msg, MsgViewHolder>(
    MsgDiffCallback()
) {

    private lateinit var context: Context
    private var itemClickListener: MsgItemClickListener? = null



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MsgViewHolder {
        this.context = parent.context
        return MsgViewHolder(
            ListItemDriverMsgBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )




    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onBindViewHolder(holder: MsgViewHolder, position: Int) {
       val model = getItem(position)
        holder.binding.root.setOnClickListener( { view ->
            model.let { itemClickListener?.click(view, model) }
        })
        holder.binding.msgTitle.text = model.title
        holder.binding.msgContent.text = model.description
        holder.binding.msgDate.text = model.createTime

    }

    fun removeList() {
        currentList.clear()
    }




    fun addItemClickListener(listener: MsgItemClickListener) {
        itemClickListener = listener
    }



}
interface MsgItemClickListener {
    fun click(view: View, model: Msg?)
}

class MsgDiffCallback : DiffUtil.ItemCallback<Msg>() {
    override fun areItemsTheSame(msgItem: Msg, newItem: Msg): Boolean {
        return msgItem.id == msgItem.id
    }

    override fun areContentsTheSame(msgItem: Msg, newItem: Msg): Boolean {
        return msgItem.id == msgItem.id
    }
}


class MsgViewHolder(val binding: ListItemDriverMsgBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind() {

    }
}